package gui;
import flexibility.CachedRowSetTableModel;
import guiCode.ReportCode;


import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.JCheckBox;
import javax.swing.SpringLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JComboBox;
import javax.swing.JLayeredPane;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;

import basic.ClientConsole;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollBar;
import javax.swing.ListSelectionModel;

import com.sun.rowset.CachedRowSetImpl;

import java.awt.ScrollPane;
import java.sql.SQLException;
import javax.swing.Box;
import javax.swing.JDesktopPane;
import javax.swing.JTextArea;
import javax.swing.JSlider;
import javax.swing.JSeparator;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;
import java.awt.Color;


public class ReportGUI extends JPanel{
	
	public JButton btnBack;
	public JComboBox comboBoxTeacher;
	private JComboBox comboBoxStudyClass;
	private   ReportCode rc;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JRadioButton rdbtnStudyClass;
	private JRadioButton rdbtnTeacher;
	private JButton btnNext;
	private boolean rdbWasPressed;
	private JTable table;
	private CachedRowSetTableModel data;
	
	private  JScrollPane scrollPane ;
	
	public ReportGUI(int x, int y, ClientConsole conn) {
		rc = new ReportCode(conn);
		initialize(360, 329);
	}
	

	
	
	void initialize(int high,int width)
	{
		table = new JTable();
		table.setCellSelectionEnabled(true);
		table.setShowGrid(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"*", "*", null, "*", null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"New column", "New column", "New column", "New column", "New column", "New column"
			}
		));
		
			
		table.getColumnModel().getColumn(0).setPreferredWidth(80);
		table.getColumnModel().getColumn(0).setMinWidth(8);
		table.getColumnModel().getColumn(1).setPreferredWidth(80);
		scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 233, 539, 93);
		table.setBounds(125, 204, 198, 114);
		add(scrollPane);
		scrollPane.setVisible(false);
/*********************************************************************************/				
		rdbWasPressed=false;
		this.setSize(559,485);
		this.setLayout(null);
		setLayout(null);
		btnBack = new JButton("Back");
		btnBack.setBounds(0, 0, 75, 32);
		add(btnBack);
/**********************************NextBtn****************************************/		
		btnNext = new JButton("Next");
		btnNext.setBounds(335, 0,75, 32);
		add(btnNext);
		btnNext.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
				if(!rdbWasPressed )
					JOptionPane.showMessageDialog(null,"Please choose one of the options!");
				TeacherOption();
			}
		});
/**********************************NextBtn END****************************************/		
		JPanel radioPanel = new JPanel();
		radioPanel.setBounds(10, 43, 105, 159);
		add(radioPanel);
		radioPanel.setLayout(new GridLayout(0, 1, 0, 0));
		add(radioPanel, BorderLayout.LINE_START);
		setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
		
		
		JLabel lblChooseGr = new JLabel("Choose grads by : ");
		radioPanel.add(lblChooseGr);
		
/**********************************TeacherRadioBtn****************************************/		
		 rdbtnTeacher = new JRadioButton("Teacher");
		
		buttonGroup.add(rdbtnTeacher);
		radioPanel.add(rdbtnTeacher);
		
		comboBoxTeacher = new JComboBox(rc.getTheacherList());
		comboBoxTeacher.setBounds(125, 110, 188, 20);
		this.add(comboBoxTeacher);
		comboBoxTeacher.setVisible(false);
		
		
		rdbtnTeacher.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent event) {
				rdbWasPressed=true;					//indicator for rdbtnTeacher was pressed!
				comboBoxTeacher.setVisible(true);
				comboBoxStudyClass.setVisible(false);
				rc.setOptionVariable((String)comboBoxTeacher.getSelectedItem(),0); //return the item selected from the comboBoxTeacher
			   // System.out.print(comboBoxTeacher.getSelectedItem()+"\n");
			}
		});


    /*********************************Study-class RadioBtn*********************************************/	

		
		rdbtnStudyClass = new JRadioButton("Study-class");
		buttonGroup.add(rdbtnStudyClass);
		radioPanel.add(rdbtnStudyClass);
		
		comboBoxStudyClass = new JComboBox(rc.getClassList());
		comboBoxStudyClass.setBounds(125, 161, 188, 20);
		add(comboBoxStudyClass);
		comboBoxStudyClass.setVisible(false);
		
		rdbtnStudyClass.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbWasPressed=true;						//indicator for rdbtnStudyClass was pressed! 
				comboBoxStudyClass.setVisible(true);
				comboBoxTeacher.setVisible(false);
				rc.setOptionVariable((String)comboBoxStudyClass.getSelectedItem(),1); //return the item selected from the comboBoxTeacher
				//System.out.print(comboBoxStudyClass.getSelectedItem()+"\n");
			}
		});
	}
	
	private void TeacherOption()
	{

	    scrollPane.setVisible(true);
	    rc.getTheacherStudyClasses();
	    //this.update(this,rc.getTheacherStudyClasses());
		
	}
/**********************************convert DB data to Table Data*******************/	
	public void update( ReportGUI rc, CachedRowSetImpl crs ){
		try {
			rc.table.setModel(data = new CachedRowSetTableModel(crs));
			DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
			centerRenderer.setHorizontalAlignment(JLabel.CENTER);
			for (int i = 0; i < rc.table.getColumnCount(); i++)
				rc.table.getColumnModel().getColumn(i)
						.setCellRenderer(centerRenderer);
			
			} catch (SQLException e) {
			 e.printStackTrace();
		}
	}
}
            

	
